package cn.frh.system.service.impl;

import cn.frh.system.domain.Role;
import cn.frh.system.mapper.RoleMapper;
import cn.frh.system.service.IRoleService;
import cn.frh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    @Transactional
    public void save(Role role) { //开始测试
        super.save(role);
        //添加当前角色的权限
        roleMapper.addPermissonsRole(role);
        //添加当前角色菜单
        roleMapper.addMenuRole(role);
    }

    @Override
    @Transactional
    public void remove(Long id) {

        //根据角色id删除当前角色权限
        roleMapper.rePermissonsByRoleId(id);
        //根据角色id删除当前角色菜单
        roleMapper.reMenuByRoleId(id);
        //根据角色id删除当前角色员工
        roleMapper.reEmployeeByRoleId(id);
        super.remove(id);
    }

    @Override
    @Transactional
    public void update(Role role) {

        //先删除 后添加

        //根据角色id删除当前角色权限
        roleMapper.rePermissonsByRoleId(role.getId());
        //根据角色id删除当前角色菜单
        roleMapper.reMenuByRoleId(role.getId());
        //添加当前角色的权限
        roleMapper.addPermissonsRole(role);
        //添加当前角色菜单
        roleMapper.addMenuRole(role);
        super.update(role);
    }
}
